package com.servlet;

import com.service.user.UserService;
import com.service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userPhone = req.getParameter("userPhone");
        String userPassword = req.getParameter("userPassword");
        String userName = req.getParameter("userName");
        String userQQ = req.getParameter("userQQ");
        String userAddress = req.getParameter("userAddress");
        String msg = "";
        /*测试*/
        System.out.println("RegisterServlet中："+userAddress+userName+userPhone+userPassword+userQQ);
        UserService userService = new UserServiceImpl();
        int Rows = userService.RegisterUser(userName, userPhone, userPassword, userQQ, userAddress);
        if(Rows > 0){
            /*注册成功*/
            msg = "注册成功";
            req.setAttribute("msg",msg);
            req.getRequestDispatcher("/jsp/login.jsp").forward(req,resp);
        }else if(Rows == -1){
            msg = "数据库中有该手机号";
            req.setAttribute("msg",msg);
            req.getRequestDispatcher("/jsp/register.jsp").forward(req,resp);
        }else{
            msg = "未知错误";
            req.setAttribute("msg",msg);
            req.getRequestDispatcher("/jsp/register.jsp").forward(req,resp);
        }
    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
